package com.example.administrator.babymusic.model;

import android.app.Notification;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;

import com.example.administrator.babymusic.constant.ActionConstant;
import com.example.administrator.babymusic.net.STError;
import com.example.administrator.babymusic.net.STHttpCallback;
import com.example.administrator.babymusic.net.STJsonQuery;
import com.example.administrator.babymusic.net.STNetTask;
import com.example.administrator.babymusic.net.STRequest;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2016/11/4 0004.
 */

public class UserModel implements IUserModel {

    private Bitmap bitmap;

    @Override
    public void getMoreFgItem(String url, final onListener listener,
                              STNetTask.BeforeQueryHook beforeQueryHook,
                              STNetTask.AfterQueryHook afterQueryHook) {
        STHttpCallback callback = new STHttpCallback() {
            @Override
            public void onSuccess(String response) {
                listener.onSuccess(response);

            }

            @Override
            public void onFailed(STError error) {
                listener.onFailure(error.getReason());

            }

        };
        STRequest request = new STRequest(url, null, null);
        STJsonQuery jsonQuery = new STJsonQuery();
        jsonQuery.setBeforeQueryHook(beforeQueryHook);
        jsonQuery.setAfterQueryHook(afterQueryHook);
        jsonQuery.post(request, callback);


    }
    public Bitmap getHttpBitmap(final String u) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection conn = null;
                try {
                    URL url = new URL(u);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(8000);
                    conn.setReadTimeout(8000);
                    InputStream in = conn.getInputStream();
                    BufferedInputStream is = new BufferedInputStream(in);
                    bitmap = BitmapFactory.decodeStream(is);
                    } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    if (conn != null){
                        conn.disconnect();
                    }
                }
            }
        }).start();
        return bitmap;
    }
    public interface onListener{
        void onSuccess(String response);
        void onFailure(String errorReason);
    }

}
